﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IPC.Communication.Framework.Core.Address
{
    public class FatekProgramAddress : DeviceAddressDataBase
    {
        public string DataCode
        {
            get;
            set;
        }

        public override void Parse(string address, ushort length)
        {
            OperateResult<FatekProgramAddress> operateResult = FatekProgramAddress.ParseFrom(address, length);
            bool isSuccess = operateResult.IsSuccess;
            if (isSuccess)
            {
                base.AddressStart = operateResult.Content.AddressStart;
                base.Length = operateResult.Content.Length;
                this.DataCode = operateResult.Content.DataCode;
            }
        }

        public override string ToString()
        {
            bool flag = this.DataCode == "X" || this.DataCode == "Y" || this.DataCode == "M" || this.DataCode == "S" || this.DataCode == "T" || this.DataCode == "C" || this.DataCode == "RT" || this.DataCode == "RC";
            string result;
            if (flag)
            {
                result = this.DataCode + base.AddressStart.ToString("D4");
            }
            else
            {
                result = this.DataCode + base.AddressStart.ToString("D5");
            }
            return result;
        }

        public static OperateResult<FatekProgramAddress> ParseFrom(string address, ushort length)
        {
            OperateResult<FatekProgramAddress> result;
            try
            {
                FatekProgramAddress fatekProgramAddress = new FatekProgramAddress();
                char c = address[0];
                char c2 = c;
                if (c2 <= 'Y')
                {
                    if (c2 <= 'D')
                    {
                        if (c2 == 'C')
                        {
                            goto IL_17F;
                        }
                        if (c2 != 'D')
                        {
                            goto IL_279;
                        }
                        goto IL_1A6;
                    }
                    else
                    {
                        if (c2 == 'M')
                        {
                            goto IL_10A;
                        }
                        switch (c2)
                        {
                            case 'R':
                                goto IL_1CD;
                            case 'S':
                                goto IL_131;
                            case 'T':
                                goto IL_158;
                            case 'U':
                            case 'V':
                            case 'W':
                                goto IL_279;
                            case 'X':
                                break;
                            case 'Y':
                                goto IL_E3;
                            default:
                                goto IL_279;
                        }
                    }
                }
                else if (c2 <= 'd')
                {
                    if (c2 == 'c')
                    {
                        goto IL_17F;
                    }
                    if (c2 != 'd')
                    {
                        goto IL_279;
                    }
                    goto IL_1A6;
                }
                else
                {
                    if (c2 == 'm')
                    {
                        goto IL_10A;
                    }
                    switch (c2)
                    {
                        case 'r':
                            goto IL_1CD;
                        case 's':
                            goto IL_131;
                        case 't':
                            goto IL_158;
                        case 'u':
                        case 'v':
                        case 'w':
                            goto IL_279;
                        case 'x':
                            break;
                        case 'y':
                            goto IL_E3;
                        default:
                            goto IL_279;
                    }
                }
                fatekProgramAddress.DataCode = "X";
                fatekProgramAddress.AddressStart = (int)Convert.ToUInt16(address.Substring(1), 10);
                goto IL_289;
            IL_E3:
                fatekProgramAddress.DataCode = "Y";
                fatekProgramAddress.AddressStart = (int)Convert.ToUInt16(address.Substring(1), 10);
                goto IL_289;
            IL_10A:
                fatekProgramAddress.DataCode = "M";
                fatekProgramAddress.AddressStart = (int)Convert.ToUInt16(address.Substring(1), 10);
                goto IL_289;
            IL_131:
                fatekProgramAddress.DataCode = "S";
                fatekProgramAddress.AddressStart = (int)Convert.ToUInt16(address.Substring(1), 10);
                goto IL_289;
            IL_158:
                fatekProgramAddress.DataCode = "T";
                fatekProgramAddress.AddressStart = (int)Convert.ToUInt16(address.Substring(1), 10);
                goto IL_289;
            IL_17F:
                fatekProgramAddress.DataCode = "C";
                fatekProgramAddress.AddressStart = (int)Convert.ToUInt16(address.Substring(1), 10);
                goto IL_289;
            IL_1A6:
                fatekProgramAddress.DataCode = "D";
                fatekProgramAddress.AddressStart = (int)Convert.ToUInt16(address.Substring(1), 10);
                goto IL_289;
            IL_1CD:
                bool flag = address[1] == 'T' || address[1] == 't';
                if (flag)
                {
                    fatekProgramAddress.DataCode = "RT";
                    fatekProgramAddress.AddressStart = (int)Convert.ToUInt16(address.Substring(2), 10);
                }
                else
                {
                    bool flag2 = address[1] == 'C' || address[1] == 'c';
                    if (flag2)
                    {
                        fatekProgramAddress.DataCode = "RC";
                        fatekProgramAddress.AddressStart = (int)Convert.ToUInt16(address.Substring(2), 10);
                    }
                    else
                    {
                        fatekProgramAddress.DataCode = "R";
                        fatekProgramAddress.AddressStart = (int)Convert.ToUInt16(address.Substring(1), 10);
                    }
                }
                goto IL_289;
            IL_279:
                throw new Exception(StringResources.Language.NotSupportedDataType);
            IL_289:
                result = OperateResult.CreateSuccessResult<FatekProgramAddress>(fatekProgramAddress);
            }
            catch (Exception ex)
            {
                result = new OperateResult<FatekProgramAddress>(ex.Message);
            }
            return result;
        }
    }
}
